<?php

namespace app\modules\merchant\services;

use app\common\services\BaseService;
use app\config\Constants;
use app\models\Merchant;
use app\modules\merchant\forms\home\UpdatePasswordForm;
use app\utils\UserUtil;

class HomeService extends BaseService{

    /**
     * 获取登陆者信息
     * @return array
     * @throws \yii\base\InvalidConfigException
     */
    public static function userInfo(){
        return UserUtil::getModuleUserIdentity("merchant");
    }

    /**
     * 修改密码
     * @param UpdatePasswordForm $form
     * @return bool
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\db\StaleObjectException
     */
    public static function updatePassword(UpdatePasswordForm $form)
    {
        /**
         * @var Merchant $model
         */
        $model = \Yii::$app->getModule("merchant")->get("user")->getIdentity();
        $credential = $model->getMerchantCredentials()->where(['type'=>Constants::USER])->one();
        $credential->setAttribute("content",\Yii::$app->getSecurity()->generatePasswordHash($form->password));
        return $credential->update() !== false;
    }
}